angular.module('starter.services', [])

.factory('DBAPI', function ($rootScope, $http, $ionicLoading, $ionicPopup, $window) {
            var base = "http://121.40.145.116:9804";
            
        //UI Indications
        $rootScope.notify = function (title, text) {
                var alertPopup = $ionicPopup.alert({
                    title: title ? title : 'Error',
                    template: text
                });
            };

            $rootScope.show = function (text) {
                $rootScope.loading = $ionicLoading.show({
                content: text ? text : 'Loading',
                animation: 'fade-in',
                showBackdrop: true,
                maxWidth: 200,
                showDelay: 0
            });
            };

            $rootScope.hide = function (text) {
                $ionicLoading.hide();
            };
 
        $rootScope.doRefresh = function (tab) {
            if(tab == 1)
                $rootScope.$broadcast('fetchAll');
            else
                $rootScope.$broadcast('fetchCompleted');
            
            $rootScope.$broadcast('scroll.refreshComplete');
        };
 
 		
	
 
        return {
            signin: function (form) {
                return $http.post(base+'/api/v1/bucketList/auth/login', form);
            },
            signup: function (form) {
                return $http.post(base+'/api/v1/bucketList/auth/register', form);
            },
            getDraws: function(email, yearParam){
            	return $http.post(base+'/api/v1/draws', {
            		token: email,
            		year: yearParam
            	});
            },
            getRootScopeToken: function(){
            		return $rootScope.getToken();
            },
            getMyEEProfile: function(email){
            	return $http.post(base+'/api/v1/myEEProfile', {
            		token: email
            	}); 
            },
            getTotalEEProfileNum: function(email, myEEScore){
            	return $http.post(base+'/api/v1/registeredNum', {
            		token: email,
            		eeScore: myEEScore
            	}); 
            },
            getScoreAboveMeNum: function(email, myEEScore){
            	return $http.post(base+'/api/v1/scoreAboveMeNum', {
            		token: email,
            		eeScore: myEEScore
            	}); 
            },
            getScoreBelowMeNum: function(email, myEEScore){
            	return $http.post(base+'/api/v1/scoreBelowMeNum', {
            		token: email,
            		eeScore: myEEScore
            	});  
            },
            updateMyname: function(email, myName){
            	return $http.post(base+'/api/v1/updateMyName', {
            		token: email,
            		name: myName
            	});
            }
       }
 })

.factory('MyLocalStorage', function ($rootScope, $window) {
	////Local Storage
 		//token
        $rootScope.setToken = function (token) {
            $window.localStorage.setItem("Token", token);
        }
 
        $rootScope.getToken = function () {
            return $window.localStorage.getItem("Token");
        }
        
        //email
        $rootScope.setMyEmail = function (email) {
            $window.localStorage.setItem("Email", email);
        }
        
        $rootScope.getMyEmail = function () {
            return $window.localStorage.getItem("Email");
        }
        
        //EE Score
        $rootScope.setMyEEScore = function (score) {
            $window.localStorage.setItem("EEScore", score);
        }
        
        $rootScope.getMyEEScore = function () {
            return $window.localStorage.getItem("EEScore");
        }
        
        //Name
        $rootScope.setMyName = function (name) {
            $window.localStorage.setItem("MyName", name);
        }
        
        $rootScope.getMyName = function () {
            return $window.localStorage.getItem("MyName");
        }
        
        //country
        $rootScope.setCountry = function (country) {
            $window.localStorage.setItem("MyCountry", country);
        }
        
        $rootScope.getCountry = function () {
            return $window.localStorage.getItem("MyCountry");
        }
        
        //NOC
        $rootScope.setMyNOC = function (noc) {
            $window.localStorage.setItem("MyNOC", noc);
        }
        
        $rootScope.getMyNOC = function () {
            return $window.localStorage.getItem("MyNOC");
        }
        
        $rootScope.isSessionActive = function () {
            return $window.localStorage.getItem("Token") ? true : false;
        }
        
        $rootScope.clearSession = function() {
        		return $window.localStorage.clear();
        }
        
        $rootScope.RegUser = {
	        email: "",
	        password: "",
	        name:"",
	        EEScore :"",
	        NOC :""
    		};
	return {
		
	}
})

.factory('Draws', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var draws = [{
    num: 1,
    drawDate: 'April 17',
    invitations: 715,
    lowestScore: 453
  }, {
    num: 1,
    drawDate: 'April 17',
    invitations: 715,
    lowestScore: 453
  }];

  return {
    all: function() {
      return draws;
    }
  };
})

.factory('Dashes', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var dashes = [{
    id: 0,
    avanta: 'img/mcfly.jpg',
    title: 'What a lovely kid!',
    postDate:'Jan 04, 2015',
    headimg: 'img/th.jpeg',
    content: 'His name is Bothen whom I met at Zambia when I was travelling there...',
    likes: '10',
    comments: '200' 
  }, {
    id: 1,
    avanta: 'img/mcfly.jpg',
    title: 'Come with me~~',
    postDate:'Jan 04, 2015',
    headimg: 'img/th.jpeg',
    content: 'Come to join my parties with the whole farmily members of Bothen',
    likes: '10',
    comments: '200'
  }];

  return {
    all: function() {
      return dashes;
    },
    remove: function(id) {
      dashes.splice(dashes.indexOf(id), 1);
    }
  };
})

.factory('Chats', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'https://pbs.twimg.com/profile_images/514549811765211136/9SgAuHeY.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'https://avatars3.githubusercontent.com/u/11214?v=3&s=460'
  }, {
    id: 2,
    name: 'Andrew Jostlin',
    lastText: 'Did you get the ice cream?',
    face: 'https://pbs.twimg.com/profile_images/491274378181488640/Tti0fFVJ.jpeg'
  }, {
    id: 3,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'https://pbs.twimg.com/profile_images/479090794058379264/84TKj_qa.jpeg'
  }, {
    id: 4,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'https://pbs.twimg.com/profile_images/491995398135767040/ie2Z_V6e.jpeg'
  }];

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
});
